package com.xnf.user.filter;

import com.xnf.model.user.pojos.ApUser;
import com.xnf.utils.AppThreadLocalUtils;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by chendx on 2022/4/1.
 */
@Order(1)
@WebFilter(filterName = "AppTokenFilter",urlPatterns = "/*")
public class AppTokenFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String userId = request.getHeader("userId");
        System.out.println("userId "+userId);
        if (userId != null && Integer.valueOf(userId) != 0){
            ApUser user = new ApUser();
            user.setId(Integer.valueOf(userId));
            AppThreadLocalUtils.set(user);
        }
        filterChain.doFilter(request,response);
    }
}
